<?php
require_once ('config.php');
require_once ('databasefunctions.php');
require_once ('displayfunctions.php');
require_once ('businessfunctions.php');
validate_user();

html_header();

switch ($_GET['action']) {
        case "reprint" :
        // reprint a bill
                create_transaction();

                // select transactions relating to that bill
                $sql = "SELECT * FROM billtransaction WHERE ";
                $sql .= "CompanyID = '" . $_GET['BillCompanyID'] . "' AND ";
                $sql .= "BillNo = '" . $_GET['ReprintNumber'] . "'";
                $sql_result = sql_execute($sql);
                $i = 0;
                while ($sql_data = mysql_fetch_array($sql_result)) {
                        $BillNumber = $sql_data['BillNo'];
                        $BillDate = $sql_data['BillDate'];
                        $amount = $sql_data['Quantity'] * $sql_data['ChargeRate'];
                        $user_date = convert_mysql_date($sql_data['TransactionDate']);
                        $user_date = substr($user_date,0,2) . "/" . substr($user_date,2,2) . "/" . substr($user_date,4,4);

                        $_SESSION['transaction'][$i] = array (
                                'TransactionID' => $sql_data['TransactionID'],
                                'CompanyID' => $sql_data['CompanyID'],
                                'ClientID' => $sql_data['ClientID'],
                                'JobID' => $sql_data['JobID'],
                                'ChargeID' => $sql_data['ChargeID'],
                                'StaffID' => $sql_data['StaffID'],
                                'TransactionDate' => $sql_data['TransactionDate'],
                                'FromTime' => $sql_data['FromTime'],
                                'ToTime' => $sql_data['ToTime'],
                                'ChargeRate' => $sql_data['ChargeRate'],
                                'Quantity' => $sql_data['Quantity'],
                                'Amount' => $amount,
                                'Remark' => $sql_data['Remark']
                        );
                        $i ++;
                }
                // if no transaction found, return to selection screen
                if (! $_SESSION['transaction']) {
                        message_box($langbilllnotexist);
                        echo "<script>history.go(-1)</script>";
                        exit;
                }
                // reprint the bill
                echo "<br>$langinvoicehead<hr>";
                print_bill($BillNumber, $BillDate);
                break;

        case "reset" :
                // AG: delete the relevant record from billdata table
                $sql = "DELETE FROM `billdata` ";
                $sql .= "WHERE `CompanyID` = '" . $_GET['BillCompanyID'] . "' AND ";
                $sql .= "`BillNo` = '" . $_GET['ResetNumber'] . "' AND ";
                $sql .= "`Year` = " . $_GET['BillYear'];
                sql_execute($sql);

                $sql = "UPDATE billtransaction SET ";
                $sql .= "BillNo = NULL, BillDate = NULL, ";
                $sql .= "UpdateUser = '" . $_SESSION['UserID'] . "', ";
                $sql .= "UpdateProgram = '" . $_SERVER['PHP_SELF'] . "' ";
                $sql .= "WHERE CompanyID = '" . $_GET['BillCompanyID'] . "' AND ";
                $sql .= "BillNo = '" . $_GET['ResetNumber'] . "' AND ";
                $sql .= "BillDate >= '" . $_GET['BillYear']."-01-01' AND ";
                $sql .= "BillDate <= '" . $_GET['BillYear']."-12-31'";
                sql_execute($sql);
                if (mysql_affected_rows() == 0) {
                        message_box($langbillnotexist);
                }
                else {
                        $message = "<br>$langbillno : " . $_GET['ResetNumber'] . " $langdeleted.<br>$langcanrebill";
                        message_box($message);
                }
                echo "<script>history.go(-1)</script>";
                break;

        case "auto" :
        // print bill automatically by selecting outstanding transactions within that period

                validate_input();

                // clear and create session variable to hold customer list to be billed
                if (session_is_registered('ClientList')) {
                        unset ($_SESSION['ClientList']);
                }
                else {
                        session_register('ClientList');
                }

                // obtain list of customers to bill within that period
                $sql = "SELECT * FROM billtransaction WHERE ";
                if ($_GET['CompanyID'] != 'ALL') {
                        $sql .= "CompanyID = '" . $_GET['CompanyID'] . "' AND ";
                }
                if (! empty($_GET['CustomerID'])) {
                        $sql .= "CustomerID = '" . $_GET['CustomerID'] . "' AND ";
                }
                $sql .= "TransactionDate >= '$DateFrom' AND ";
                $sql .= "TransactionDate <= '$DateTo' AND BillNo IS NULL ORDER BY CompanyID, ClientID ";
                $sql_result = sql_execute($sql);

                if (mysql_affected_rows() == 0) {
                        message_box($langnotransaction);
                        echo "<script>history.go(-1)</script>";
                        exit;
                }
                else {
                        // create a variable to hold all client transactions
                        $last_company = '';
                        $last_client = '';
                        while ($sql_data = mysql_fetch_array($sql_result)) {
                                $amount = $sql_data['Quantity'] * $sql_data['ChargeRate'];
                                $client = $sql_data['ClientID'];
                                $company = $sql_data['CompanyID'];
                                if (($last_client != $client) || ($last_company != $company)) {
                                        $i = 0;
                                        $last_client = $client;
                                        $last_company = $company;
                                }
                                $_SESSION['ClientList'][$company][$client][$i] = array (
                                        'TransactionID' => $sql_data['TransactionID'],
                                        'CompanyID' => $sql_data['CompanyID'],
                                        'ClientID' => $sql_data['ClientID'],
                                        'JobID' => $sql_data['JobID'],
                                        'ChargeID' => $sql_data['ChargeID'],
                                        'StaffID' => $sql_data['StaffID'],
                                        'TransactionDate' => $sql_data['TransactionDate'],
                                        'FromTime' => $sql_data['FromTime'],
                                        'ToTime' => $sql_data['ToTime'],
                                        'ChargeRate' => $sql_data['ChargeRate'],
                                        'Quantity' => $sql_data['Quantity'],
                                        'Amount' => $amount,
                                        'Remark' => $sql_data['Remark']
                                );
                                $i ++;
                        }

                        create_transaction();

                        // get bill date
                        $datestamp = getdate();
                        $datebill = $datestamp['year'] . "-" . sprintf('%02d',$datestamp['mon']) .
                                                "-" . sprintf('%02d',$datestamp['mday']);

                        echo "<br>$langinvoicehead<hr>";

                        // create session variable 'transaction' to hold transaction of a particular company / customer
                        foreach ($_SESSION['ClientList'] as $client_bill) {
                                foreach ($client_bill as $client) {
                                        // get bill number
                                        $billnumber = get_next_number($client[0]['CompanyID']);
                                        $_SESSION['transaction'] = $client ;
                                        print_bill($billnumber, $datebill);
                                        echo "<hr>";
                                }
                        }
                }

                break;

        case "manual" :
        // display list of un-bill transaction for selection

                $DateFrom = '';
                $DateTo = '';

                validate_input();
                create_transaction();

                // display un-billed items
                $sql = "SELECT * FROM billtransaction WHERE ";
                $sql .= "ClientID = '" . $_GET['ClientID'] . "' AND ";
                $sql .= "CompanyID = '" . $_GET['CompanyID'] . "' AND ";
                $sql .= "TransactionDate >= '$DateFrom' AND ";
                $sql .= "TransactionDate <= '$DateTo' AND BillNo IS NULL";

                $sql_result = sql_execute($sql);

                if (mysql_affected_rows() == 0) {
                        message_box($langnotransaction);
                        echo "<script>history.go(-1)</script>";
                        exit;
                }
                else {
                        // display form title
                        ?>
                        <br><br><? echo $langmarkitem ?><br><br>
                        <form method='GET' action='billprinting.php'>
                        <table>
                                <tr>
                                        <td><? echo $langbillno ?>
                                        <td><input type = 'text' name = 'billno'> <? echo $langemptyautogen ?>
                                <tr>
                                        <td><? echo $langbilldate ?>
                                        <td><input type = 'text' name = 'billdate'> ddMMyyyy <? echo $langemptytoday ?>
                        </table>
                        <table>
                        <tr bgcolor='<? echo $color1 ?>'>
                                <td>
                                <td><? echo $langcompanyid ?>
                                <td><? echo $langclientid ?>
                                <td><? echo $langjobid ?>
                                <td><? echo $langchargeid ?>
                                <td><? echo $langstaffid ?>
                                <td><? echo $langtransactiondate ?>
                                <td><? echo $langtimefrom ?>
                                <td><? echo $langtimeto ?>
                                <td><? echo $langprice ?>
                                <td><? echo $langqtyhour ?>
                                <td><? echo $langamount ?>
                        <?

                        // list un-bill transaction
                        $i = 0;
                        while ($sql_data = mysql_fetch_array($sql_result)) {
                                $amount = $sql_data['Quantity'] * $sql_data['ChargeRate'];
                                $user_date = convert_mysql_date($sql_data['TransactionDate']);
                                $user_date = substr($user_date,0,2) . "/" . substr($user_date,2,2) . "/" . substr($user_date,4,4);
                                echo "<tr>";
                                echo "<td><input type = 'checkbox' name = 'item$i' checked>";
                                echo "<td>" . $sql_data['CompanyID'];
                                echo "<td>" . $sql_data['ClientID'];
                                echo "<td>" . $sql_data['JobID'];
                                echo "<td>" . $sql_data['ChargeID'];
                                echo "<td>" . $sql_data['StaffID'];
                                echo "<td>" . $user_date;
                                echo "<td>" . $sql_data['FromTime'];
                                echo "<td>" . $sql_data['ToTime'];
                                echo "<td>" . $sql_data['ChargeRate'];
                                echo "<td>" . $sql_data['Quantity'];
                                echo "<td>" . $amount ;

                                $_SESSION['transaction'][$i] = array (
                                        'TransactionID' => $sql_data['TransactionID'],
                                        'CompanyID' => $sql_data['CompanyID'],
                                        'ClientID' => $sql_data['ClientID'],
                                        'JobID' => $sql_data['JobID'],
                                        'ChargeID' => $sql_data['ChargeID'],
                                        'StaffID' => $sql_data['StaffID'],
                                        'TransactionDate' => $sql_data['TransactionDate'],
                                        'FromTime' => $sql_data['FromTime'],
                                        'ToTime' => $sql_data['ToTime'],
                                        'ChargeRate' => $sql_data['ChargeRate'],
                                        'Quantity' => $sql_data['Quantity'],
                                        'Amount' => $amount,
                                        'Remark' => $sql_data['Remark']
                                );
                                $i ++;
                        }
                        ?>
                        </table>
                        <input type = 'hidden' name = 'action' value = 'print'>
                        <input type = 'submit' value = '<? echo $langgeneratebill ?>'>
                        <form>
                        <?
                }
                break;

        case "print" :
        // modify user selected transactions for printing

                // move record pointer in $_SESSION['transaction'] to the 1st record
                reset ($_SESSION['transaction']);
                $temp = current($_SESSION['transaction']);



                // validate bill number
                if (! empty($_GET['billno'])) {
                        // check duplicate bill number
                        $sql = "SELECT count(*) as billitems FROM billtransaction WHERE BillNo = '" . $_GET['billno'] . "'";
                        $sql_result = sql_execute($sql);
                        $sql_data = mysql_fetch_array($sql_result);
                        if ($sql_data["billitems"] != 0) {
                                $error_text .= "<br>- $langduplicatebillno<br>";
                        }
                        else $billnumber = $_GET['billno'];
                }
                else {
                        // get next unused bill number
                        $billnumber = get_next_number($temp['CompanyID']);
                }

                // validate bill date
                if (! empty($_GET['billdate'])) {
                        $datebill = validate_date($_GET['billdate'], 1);
                }
                else {
                        $datestamp = getdate();
                        $datebill = $datestamp['year'] . "-" . sprintf('%02d',$datestamp['mon']) .
                                                "-" . sprintf('%02d',$datestamp['mday']);
                }
                if (! $datebill) {
                        $error_text .= '<br>- $langinvaliddate<br>';
                }
                $user_bill_date = convert_mysql_date($datebill);

                // display error and return if any
                if ($error_text) {
                        message_box($error_text);
                        ?>
                        <script>history.go(-1)</script>
                        <?
                        exit;
                }

                // obtain all HTTP 'GET' keys
                $var_from_http_get = array_keys($_GET);

                // extract all keys which starts with 'item' and store them into $bill
                foreach ($var_from_http_get as $item) {
                        if (substr($item,0,4) == 'item') {
                                $line_no = substr($item, 4);
                                $bill[] = $line_no;
                        }
                }

                // erase array values in session variable 'transaction' which are not in $bill variable
                // i.e. erase transactions not selected for billing
                foreach ($_SESSION['transaction'] as $index => $content) {
                        $record_found = 0;
                        foreach ($bill as $value) {
                                if ($value == $index) $record_found = 1;
                        }
                        if ($record_found == 0) {
                                unset ($_SESSION['transaction'][$index]);
                        }
                }

                // call printing function
                echo "<br>$langinvoicehead<hr>";
                print_bill($billnumber, $datebill);
                exit;
                break;

        default :
        // display client / date / company selection form
                ?>
                <table>
                <tr bgcolor='<? echo $color3 ?>' align="center">
                        <td abbr='20%' ><b><? echo $langbillmanagement ?>
                        <td width='10%'><font color='$color4'><a href = 'billprinting.php'><? echo $langmanagement?></a>
                        <td width='10%'><font color='$color4'><a href = 'bill_list.php'><? echo $langsearch?></a>
                </table>
                <SCRIPT LANGUAGE="JavaScript" ID="js1">
                var cal1 = new CalendarPopup();
                </SCRIPT>
                <br><i><? echo $langspecialnormal ?></i><br><br>
                <form name='selectform'>
                <table>
                        <tr>
                                <td colspan = '2' bgcolor='<? echo $color1 ?>'><? echo $langnormaloperation ?>
                        <tr>
                                <td bgcolor='<? echo $color1 ?>'><? echo $langcompanyid?>
                                <td><select name='CompanyID'>
                                        <?
                                                $company_list = get_company_list();
                                                foreach ($company_list as $company) {
                                                        echo "<option value='" . $company['CompanyID'] . "'>";
                                                        echo $company['CompanyID'] . " - " . $company['CompanyName'];
                                                }
                                        ?>
                                        </select>
                        <tr>
                                <td bgcolor='<? echo $color1 ?>'><? echo $langyear ?>
                                <td><input type = 'text' name = 'BillYear' value='<? echo date("Y") ?>'>
                        <tr>
                                <td bgcolor='<? echo $color1 ?>'><? echo $langclientid ?>
                                <td><input type = 'text' name = 'ClientID'>
                                        <input type = 'button' value = 'S' onclick=opensearch('client','selectform.ClientID')>
                        <tr>
                                <td bgcolor='<? echo $color1 ?>'><? echo $langbillmethod ?>
                                <td><select name = 'billmethod'>
                                                <option value = 'manual'><? echo $langmanualbill ?>
                                                <option value = 'auto'><? echo $langautobill ?>
                                        </select>
                        <tr>
                                <td bgcolor='<? echo $color1 ?>'><? echo $langdatefrom ?>
                                <td><input type = 'text' name = 'FromDate'>
                                <A HREF="#" onClick="cal1.select(document.forms[0].FromDate,'anchor1','ddMMyyyy'); return false;" TITLE="Date" NAME="anchor1" ID="anchor1">
                                 <img src="calendar.gif" border=0></A>
                        <tr>
                                <td bgcolor='<? echo $color1 ?>'><? echo $langdateto ?>
                                <td><input type = 'text' name = 'ToDate'>
                                <A HREF="#" onClick="cal1.select(document.forms[0].ToDate,'anchor1','ddMMyyyy'); return false;" TITLE="Date" NAME="anchor1" ID="anchor1">
                                <img src="calendar.gif" border=0></A>
                        <tr>
                                <td colspan = '2' bgcolor='<? echo $color1 ?>'><? echo $langspecialoperation ?>
                        <tr>
                                <td bgcolor='<? echo $color1 ?>'><? echo $langcompanyid?>
                                <td><select name='BillCompanyID'>
                                        <?
                                                $company_list = get_company_list();
                                                foreach ($company_list as $company) {
                                                        echo "\n<option value='" . $company['CompanyID'] . "'>";
                                                        echo $company['CompanyID'] . " - " . $company['CompanyName'];
                                                }
                                        ?>
                                        </select>
                        <tr>
                                <td bgcolor='<? echo $color1 ?>'><? echo $langreprintbill ?>
                                <td><input type = 'text' name = 'ReprintNumber'>
                        <tr>
                                <td bgcolor='<? echo $color1 ?>'><? echo $langresetbill ?>
                                <td><input type = 'text' name = 'ResetNumber'>
                        <!-- AG Change 20.11.2005 -->
                        <tr>
                                <td colspan = '2' bgcolor='<? echo $color1 ?>'><? echo $langnewoperation ?>
                        <tr>
                                <td bgcolor='<? echo $color1 ?>'><? echo $langbillprintcustom ?>
                                <td><input type = 'text' name = 'CustomNumber'>
                        <tr>
                                <td bgcolor='<? echo $color1 ?>'><? echo $langbillsumexp ?>
                                <td><select size="1" name="SumExp">
                                        <option value="t"><? echo $langtotal?></option>
                                        <option value="e"><? echo $langexpensetype?></option>
                                        <option value="n"><? echo $langno?></option>
</select>
                </table>
                <input type = 'button' value = '<? echo $langexecute ?>' onclick='goaction()'>
                </form>

                <script>
                function opensearch(search_type, fieldname) {
                                url_text = 'search.php?master=' + search_type + '&inputname=' + fieldname;
                                window.open(url_text ,'','width=700,height=500');
                }

                function goaction() {
                        // if no value input in Reset number and Reprint number, print the bills
                        if (! document.selectform.ReprintNumber.value && ! document.selectform.ResetNumber.value
                              && ! document.selectform.CustomNumber.value) {
                        // create difference href string based on user action
                                hrefstring = 'billprinting.php?ClientID=';
                                hrefstring = hrefstring + document.selectform.ClientID.value;
                                hrefstring = hrefstring + '&FromDate=' + document.selectform.FromDate.value;
                                hrefstring = hrefstring + '&ToDate=' + document.selectform.ToDate.value;
                                hrefstring = hrefstring + '&action=';

                                action = document.selectform.billmethod.selectedIndex;
                                actionvalue = document.selectform.billmethod.options[action].value;

                                company = document.selectform.CompanyID.selectedIndex;
                                companyid = document.selectform.CompanyID.options[company].value;

                                this.location = hrefstring + actionvalue + '&CompanyID=' + companyid;
                        }
                        else {
                                company = document.selectform.BillCompanyID.selectedIndex;
                                companyid = document.selectform.BillCompanyID.options[company].value;

                                if (document.selectform.ReprintNumber.value) {
                                // reprint bill
                                        hrefstring = 'billprinting.php';
                                        hrefstring = hrefstring + '?ReprintNumber=' + document.selectform.ReprintNumber.value;
                                        this.location = hrefstring + '&action=reprint&BillCompanyID=' + companyid ;

                                }
                                else if (document.selectform.ResetNumber.value) {
                                // reset bill number
                                        hrefstring =  'billprinting.php';
                                        hrefstring += '?ResetNumber=' + document.selectform.ResetNumber.value;
                                        hrefstring += '&action=reset&BillCompanyID=' + companyid;
                                        hrefstring += '&BillYear=' + document.selectform.BillYear.value;;
                                        this.location = hrefstring;
                                }
                                else if (document.selectform.CustomNumber.value) {
                                // Custom bill printing (AG Change 20.11.2005)
                                        hrefstring = 'mybillprinting.php';
                                        hrefstring = hrefstring + '?CustomNumber='  + document.selectform.CustomNumber.value
                                                                + '&SumExp='        + document.selectform.SumExp.value
                                                                + '&BillYear='      + document.selectform.BillYear.value
                                                                + '&BillCompanyID=' + companyid;
                                        // this.location = hrefstring + '&action=reset&BillCompanyID=' + companyid;
                                        window.open(hrefstring);
                                }


                        }
                }
                </script>
                <?
                break;
}

html_footer();


function validate_input() {
// validate user input for billing range
        global $DateFrom, $DateTo, $langinvalidclient, $langinvaliddate, $langfrom, $langto, $langinvaliddaterange;

        // if client ID is empty and action is auto, no need to check client ID
        if (! (empty($_GET['ClientID']) && $_GET['action'] == 'auto')) {
                if (! validate_client($_GET['ClientID'])) {
                        $error_text .= "<br>- $langinvalidclient<br>";
                }
        }

        $DateFrom = validate_date($_GET['FromDate'], 1);
        if (! $DateFrom) {
                $error_text .= "<br>- $langinvaliddate $langfrom<br>";
        }

        $DateTo = validate_date($_GET['ToDate'], 1);
        if (! $DateTo) {
                $error_text .= "<br>- $langinvaliddate $langto<br>";
        }

        if ($DateFrom && $DateTo & ($DateFrom > $DateTo)) {
                $error_text .= "<br>- $langinvaliddaterange<br>";
        }

        if ($error_text) {
                message_box($error_text);
                ?>
                <script>history.go(-1)</script>
                <?
                exit;
        }
}

function print_bill ($bill_number, $bill_date) {
// displaying the bill in html
        global $langinvoice, $langclientid, $langclientname, $langaddress, $langbillno, $langbilldate,
                   $langjobid, $langchargeid, $langstaffid, $langtransactiondate, $langtimefrom, $langtimeto,
                   $langprice, $langqtyhour, $langamount, $langtotal, $langreference;

        // AG: Save bill year
        $bill_year = substr($bill_date,0,4);

        // convert bill date format from YYYYMMDD to user profile format
        $user_date = convert_mysql_date($bill_date);
        $user_date = substr($user_date,0,2) . "/" . substr($user_date,2,2) . "/" . substr($user_date,4,4);

        reset ($_SESSION['transaction']);
        $temp = current($_SESSION['transaction']);

        // get company name
        $sql = "SELECT CompanyName FROM companymaster WHERE CompanyID = '" ;
        $sql .= $temp['CompanyID'] . "'";
        $sql_result = sql_execute($sql);
        $sql_data = mysql_fetch_array($sql_result);
        $companyname = $sql_data['CompanyName'];

        // get client name
        $sql = "SELECT * FROM clientmaster WHERE ClientID = '" ;
        $sql .= $temp['ClientID'] . "'";
        $sql_result = sql_execute($sql);
        $sql_data = mysql_fetch_array($sql_result);

        ?>
        <h3 align="center"><? echo "$companyname <br>$langinvoice" ?></h3>
        <table>
                <tr>
                        <td bgcolor='<? echo $color1 ?>'><? echo $langclientid ?>
                        <td><? echo $temp['ClientID'] ?>
                <tr>
                        <td bgcolor='<? echo $color1 ?>'><? echo $langclientname ?>
                        <td><? echo $sql_data['ClientName'] ?>
                <tr>
                        <td bgcolor='<? echo $color1 ?>'><? echo $langaddress ?>
                        <td><? echo $sql_data['Address1'] ?>
                <tr>
                        <td>
                        <td><? echo $sql_data['Address2'] ?>
                <tr>
                        <td>
                        <td><? echo $sql_data['Address3'] ?>
                <tr>
                        <td>
                        <td><? echo $sql_data['Address4'] ?>
                <tr>
                        <td bgcolor='<? echo $color1 ?>'><? echo $langbillno ?>
                        <td><? echo $bill_number ?>
                <tr>
                        <td bgcolor='<? echo $color1 ?>'><? echo $langbilldate ?>
                        <td><? echo $user_date ?>
        </table>
        <br>
        <table>
        <tr bgcolor='<? echo $color1 ?>' align="center">
                <td><? echo $langjobid ?>
                <td><? echo $langchargeid ?>
                <td><? echo $langstaffid ?>
                <td><? echo $langtransactiondate ?>
                <td><? echo $langtimefrom ?>
                <td><? echo $langtimeto ?>
                <td><? echo $langprice ?>
                <td><? echo $langqtyhour ?>
                <td><? echo $langamount ?>
        <?

        // list un-bill transaction
        $total_amount = 0;
        foreach ($_SESSION['transaction'] as $content) {
                $user_date = convert_mysql_date($content['TransactionDate']);
                $user_date = substr($user_date,0,2) . "/" . substr($user_date,2,2) . "/" . substr($user_date,4,4);
                echo "\n<tr>";
                echo "<td>" . $content['JobID'];
                echo "<td>" . $content['ChargeID'];
                echo "<td>" . $content['StaffID'];
                echo "<td align='right'>" . $user_date;
                echo "<td align='right'>" . substr($content['FromTime'],0,5);
                echo "<td align='right'>" . substr($content['ToTime'],0,5);
                echo "<td align='right'>" . $content['ChargeRate'];
                echo "<td align='right'>" . $content['Quantity'];
                echo "<td align='right'>";
                printf('%.2f', $content['Amount']) ;
                $total_amount += $content['Amount'];

                if (! empty($content['Remark'])) {
                        echo "\n<tr><td><td align='right'>$langreference :<td colspan='6'>" . $content['Remark'];
                }
                echo "\n<tr><td colspan='9'>";

                // update bill date / number to bill transaction
                $sql = "UPDATE billtransaction SET BillDate = '$bill_date', BillNo = '$bill_number', ";
                $sql .= "UpdateUser = '" . $_SESSION['UserID'] . "', ";
                $sql .= "UpdateProgram = '" . $_SERVER['PHP_SELF'] . "' ";
                $sql .= " WHERE TransactionID = " . $content['TransactionID'] ;

                sql_execute($sql);


        }
        // AG: insert a record in billdata table
        $sql = "INSERT billdata (CompanyID, Year, BillNo) VALUES ";
        $sql .="('".$temp['CompanyID']."','$bill_year','$bill_number')";
        sql_execute($sql);

        echo "\n";
        ?>
        <tr>
                <td align = 'right' colspan = '8'><? echo $langtotal ?>
                <td align = 'right'><? printf('%.2f', $total_amount) ?>
        </table>
        <?
}

function create_transaction() {
        // create temp session variable
        if (session_is_registered('transaction')) {
                unset ($_SESSION['transaction']);
        }
        else {
                session_register('transaction');
        }
}



?>
